from django.db import models
from django_celery_beat.models import PeriodicTask, CrontabSchedule

from utils.base import BaseModel


class Suite(BaseModel):

    name = models.CharField(verbose_name='套件名称', max_length=50)
    project = models.ForeignKey('projects.Project', on_delete=models.PROTECT)
    conditions = models.JSONField(verbose_name='套件条件')

    class Meta:
        ordering = ['-create_time']
        db_table = 'tb_suite'
        verbose_name = '套件表'
        verbose_name_plural = verbose_name


class CrontabTask(BaseModel):
    """
    定时任务模型
    """
    project = models.ForeignKey('projects.Project', verbose_name='所属项目', on_delete=models.CASCADE)
    suite = models.ForeignKey(Suite, verbose_name='套件名称', on_delete=models.CASCADE)
    env = models.ForeignKey('envs.Env', verbose_name='环境名称', on_delete=models.PROTECT)
    crontab = models.CharField('定时策略', max_length=256)
    enabled = models.BooleanField(default=True, verbose_name='启用', help_text='是否启用')
    periodic_task = models.OneToOneField(PeriodicTask, on_delete=models.PROTECT, null=True, blank=True)

    class Meta:
        db_table = 'tb_crontab_task'
        verbose_name = '定时任务'
        verbose_name_plural = verbose_name
        ordering = ['-create_time']



